package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.HomeResBean;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.widget.Flowlayout;
import com.zzhou.common.library.util.StringUtil;
import com.zzhou.common.library.util.ToastShow;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Mr'Tang on 2018/12/19.
 */

public class HomeResAdapter extends XRecyclerView.Adapter<HomeResAdapter.ViewHolder> {
    private List<HomeResBean> mlist;
    private Context mcontext;

    public HomeResAdapter(Context context, List<HomeResBean> datas) {
        this.mcontext = context;
        this.mlist = datas;
    }

    public void addAll(List<HomeResBean> data, boolean append) {
        if (mlist != null) {
            if (!append) {
                mlist.clear();
            }
            mlist.addAll(data);
        } else {
            mlist = data;
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_home_res, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        HomeResBean bean = mlist.get(position);
        ImageLoader.loadImg(mcontext, bean.getResicon(), viewHolder.item_home_res_pic);
        viewHolder.item_home_res_title.setText(bean.getResname());
        viewHolder.item_home_res_year_readnum.setText("版本：" + bean.getYear() + "    " + bean.getReadnum() + "人浏览");
        viewHolder.item_home_res_flow.removeAllViews();
        if (!StringUtil.isNullOrEmpty(bean.getSign())) {
            List<String> flow = Arrays.asList(bean.getSign().split(","));
            for (int i = 0; i < flow.size(); i++) {
                TextView myText = new TextView(mcontext);
                myText.setTextSize(12);
                myText.setTextColor(Color.parseColor("#14abfd"));
                myText.setText(flow.get(i));
                myText.setBackgroundResource(R.drawable.flow_textview_bg);
                ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                lp.setMargins(0, 10, 10, 10);
                viewHolder.item_home_res_flow.addView(myText, lp);
            }
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastShow.show(mcontext, "待添加");
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView item_home_res_pic;
        TextView item_home_res_title;
        TextView item_home_res_year_readnum;
        Flowlayout item_home_res_flow;

        public ViewHolder(View convertView) {
            super(convertView);
            item_home_res_pic = convertView.findViewById(R.id.item_home_res_pic);
            item_home_res_title = convertView.findViewById(R.id.item_home_res_title);
            item_home_res_year_readnum = convertView.findViewById(R.id.item_home_res_year_readnum);
            item_home_res_flow = convertView.findViewById(R.id.item_home_res_flow);
        }
    }
}